package cn.walls1717.juc.interrupt;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * 测试线程是否会被打断
 *
 * 调用线程interrupt方法并不会直接停止该线程，只是将中断标志设置为true，要线程自己根据标志配合中断。
 *
 * @author walls1717
 * @create 2022/12/10 14:28
 **/
@Slf4j
public class Interrupt2Demo {

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i <= 300; i++) {
                log.info("循环打印次数 —> {}", i);
            }
            log.info("中断标志 -> {}", Thread.currentThread().isInterrupted());
        }, "t1");
        t1.start();

        log.info("t1线程默认的中断标志 -> {}", t1.isInterrupted());
        TimeUnit.MILLISECONDS.sleep(1);
        t1.interrupt();
        log.info("t1修改后的中断标志 -> {}", t1.isInterrupted());
    }
}
